#!/usr/bin/env python
# coding: utf-8

# # SFTPClient用于连接远程服务器并执行上传下载

# In[1]:


import paramiko, os, shutil, logging


# In[2]:


def delfiles():
    currentpath = os.getcwd()

    # 删除原文件
    gauge = os.path.join(currentpath, 'EJanus2-hmi-Gague')
    point = os.path.join(currentpath, 'EJanus2-hmi-point')
    rcc = os.path.join(currentpath, 'rcc')

    if os.path.exists(gauge):
        os.unlink(gauge)
    if os.path.exists(point):
        os.unlink(point)
    if os.path.exists(rcc):
        shutil.rmtree(rcc)


# In[3]:


def downfiles():
    currentpath = os.getcwd()

    # 删除原文件
    delfiles()

    # 实例化一个transport对象
    transport = paramiko.Transport(('10.1.74.110', 22))
    # 建立连接
    transport.connect(username='cluster', password='cluster')
    # 实例化一个 sftp对象,指定连接的通道
    sftp = paramiko.SFTPClient.from_transport(transport)

    # 下载文件
    sftp.get('/home/cluster/chenchao/SOC/release/bin/EJanus2-hmi-Gague', 'EJanus2-hmi-Gague')
    sftp.get('/home/cluster/chenchao/SOC/release/bin/EJanus2-hmi-point', 'EJanus2-hmi-point')

    data = [
        {'path': 'rcc', 'type': 'dir'},
        ##### gauge
        {'path': 'rcc/ScenePlugins', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins/OrangeSkin', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins/SigmaSkin', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins/SingleSkin', 'type': 'dir'},    
        {'path': 'rcc/ScenePlugins/OrangeSkin/rccs', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins/SigmaSkin/rccs', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins/SingleSkin/rccs', 'type': 'dir'},

        {'path': 'rcc/config.json', 'type': 'file'},
        {'path': 'rcc/ScenePlugins/OrangeSkin/manifest.json', 'type': 'file'},
        {'path': 'rcc/ScenePlugins/SigmaSkin/manifest.json', 'type': 'file'},
        {'path': 'rcc/ScenePlugins/SingleSkin/manifest.json', 'type': 'file'},

        {'path': 'rcc/ScenePlugins/OrangeSkin/rccs/orangeimage.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins/OrangeSkin/rccs/orangeskin.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins/SigmaSkin/rccs/sigmaimage.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins/SigmaSkin/rccs/sigmaskin.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins/SingleSkin/rccs/singleimage.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins/SingleSkin/rccs/singleskin.rcc', 'type': 'file'},


        ##### point
        {'path': 'rcc/ScenePlugins1', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins1/OrangeSkin', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins1/SigmaSkin', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins1/SingleSkin', 'type': 'dir'},    
        {'path': 'rcc/ScenePlugins1/OrangeSkin/rccs', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins1/SigmaSkin/rccs', 'type': 'dir'},
        {'path': 'rcc/ScenePlugins1/SingleSkin/rccs', 'type': 'dir'},

        {'path': 'rcc/config1.json', 'type': 'file'},
        {'path': 'rcc/ScenePlugins1/OrangeSkin/manifest.json', 'type': 'file'},
        {'path': 'rcc/ScenePlugins1/SigmaSkin/manifest.json', 'type': 'file'},
        {'path': 'rcc/ScenePlugins1/SingleSkin/manifest.json', 'type': 'file'},

        {'path': 'rcc/ScenePlugins1/OrangeSkin/rccs/orangeimage.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins1/OrangeSkin/rccs/orangeskin.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins1/SigmaSkin/rccs/sigmaimage.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins1/SigmaSkin/rccs/sigmaskin.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins1/SingleSkin/rccs/singleimage.rcc', 'type': 'file'},
        {'path': 'rcc/ScenePlugins1/SingleSkin/rccs/singleskin.rcc', 'type': 'file'},

    ]

    for one in data:
        if one.get('type') == 'dir':
            os.makedirs(os.path.join(currentpath, one.get('path')), exist_ok=True)
        elif one.get('type') == 'file':
            sftp.get('/home/cluster/chenchao/SOC/release/'+one.get('path'), os.path.join(currentpath, one.get('path')))

    transport.close()


# In[18]:


def checkUSB():
    if os.path.exists('G:'):
        os.chdir('G:')
        return True
    return False


# In[21]:


if __name__ == '__main__':
    logging.basicConfig(level=logging.WARNING, format='%(asctime)s - %(levelname)s - %(message)s')
    logging.disable(logging.INFO)

    if checkUSB():
        logging.warning('start')
        downfiles()
        logging.warning('end')
    else:
        logging.warning('不存在USB')


# In[ ]:




